import { createRouter, createWebHistory } from 'vue-router'
import MainLayout from './components/MainLayout.vue'
import Login from './components/Login.vue'
import Home from './views/Home.vue'
import NewsPublish from './views/NewsPublish.vue'
import PublishedNews from './views/PublishedNews.vue'
import SystemManagement from './views/SystemManagement.vue'
import LogManagement from './views/LogManagement.vue'
import NotificationManagement from './views/NotificationManagement.vue'
import InternationalizationManagement from './views/InternationalizationManagement.vue'
import NewsPublishSetting from './views/NewsPublishSetting.vue'
import Preview from './views/Preview.vue'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/login',
            name: 'Login',
            component: Login,
            meta: { requiresAuth: false }
        },
        {
            path: '/',
            component: MainLayout,
            meta: { requiresAuth: true },
            children: [
                { path: '', redirect: 'home' },
                {
                    path: 'home',
                    name: 'home',
                    component: Home
                },
                {
                    path: 'news-publish',
                    name: 'newsPublish',
                    component: NewsPublish,
                },
                {
                    path: 'setting',
                    name: 'newsPublishSetting',
                    component: NewsPublishSetting
                
                },
                {
                    path: 'preview',
                    name: 'preview',
                    component: Preview
                
                },
                {
                    path: 'published-news',
                    name: 'publishedNews',
                    component: PublishedNews
                },
                {
                    path: 'system-management',
                    name: 'systemManagement',
                    component: SystemManagement
                },
                {
                    path: 'log-management',
                    name: 'logManagement',
                    component: LogManagement
                },
                {
                    path: 'notification-management',
                    name: 'notificationManagement',
                    component: NotificationManagement
                },
                {
                    path: 'internationalization-management',
                    component: InternationalizationManagement
                }
            ]
        }
    ]
})

// 4. 路由守卫：控制登录权限
router.beforeEach((to, from, next) => {
    // 判断当前路由是否需要登录（通过 meta.requiresAuth 标记）
    if (to.meta.requiresAuth) {
        // 检查 localStorage 中是否有令牌
        const token = localStorage.getItem('access_token')
        if (token) {
            // 有令牌：允许访问业务页
            next()
        } else {
            // 无令牌：强制跳转到登录页
            next('/login')
        }
    } else {
        // 无需登录的页面（如登录页）：直接放行
        next()
    }
})

export default router